package com.yeyouluo.activemq.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class AppProducer {

	private static final String url = "tcp://127.0.0.1:61616";
	private static final String queueName = "first-queue";

	public static void main(String[] args) {
		// 1.创建ConnectionFactory
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, url);

		// 2.创建连接
		Connection connection = null;
		try {
			connection = connectionFactory.createConnection();
			// 3.启动连接
			connection.start();

			// 4.创建Session
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

			// 5.创建Session
			Destination destination = session.createQueue(queueName);

			// 6.创建提供者
			MessageProducer producer = session.createProducer(destination);

			for (int i = 0; i < 100; i++) {
				// 7.创建消息
				TextMessage textMessage = session.createTextMessage("yeyouluo" + (i + 1));

				// 8.发送消息
				producer.send(textMessage);

				System.out.println("生产消息：" + textMessage.getText());
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 9.关闭连接
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

	}

}
